package com.fenghongzhang.yuekao;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class MusicUtil {

    private static final String TAG = "MusicUtil";

    //数据,让listview来用
    public static List<Song> getMusic(Context context){
        List<Song> list = new ArrayList<>();

        ContentResolver contentResolver = context.getContentResolver();

        Cursor query = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        if(query != null){

            while (query.moveToNext()){
                String title = query.getString(query.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String artist = query.getString(query.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String duration = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DURATION));
                String data = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DATA));
                String size = query.getString(query.getColumnIndex(MediaStore.Audio.Media.SIZE));
                String aid = query.getString(query.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));


                //过滤段玲生
                if (Integer.parseInt(duration) > 1000 * 60){
                    Song song = new Song(title, artist, duration, data, size, aid);
                    list.add(song);
                }

            }
            query.close();
        }
        return list;
    }

    public static String getAlbum(Context context, int id) {
        String mUriAlbums = "content://media/external/audio/albums";
        String[] projection = new String[]{"album_art"};
        ContentResolver contentResolver = context.getContentResolver();
        Cursor query = contentResolver.query(Uri.parse(mUriAlbums + "/" + id), projection, null, null, null);
        if (query != null) {
            //过滤一下短音频
            if (query.getCount() > 0 && query.getColumnCount() > 0) {
                query.moveToNext();
                String string = query.getString(0);
                Log.i(TAG, "getAlbum: " + string);
                return string;
            }
            query.close();
        }

        return null;

    }
}
